﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Security;

namespace BeniBastanYaratWinFormProject
{
    public partial class Odeme : Form
    {
        public Odeme()
        {
            InitializeComponent();
        }
        SqlConnection cnn = new SqlConnection("Server=mssql.mustafadindar.com;Database=BeniBastanYaratDB;User Id=Admin3207;Password=Y1234567?");
        SqlDataAdapter adp;
        DataTable dt;
        private void Odeme_Load(object sender, EventArgs e)
        {
            SqlDataAdapter MyAdp = new SqlDataAdapter("Select UrunID, UrunAdi FROM Urunler", cnn);
            DataTable Mydt = new DataTable();
            MyAdp.Fill(Mydt);
            comboBox1.DisplayMember = "UrunAdi";
            comboBox1.ValueMember = "UrunID";
            comboBox1.DataSource = Mydt;
            dataGridView1.DataSource = dt;

        }
        //
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.SelectedValue != null)
                {
                    adp = new SqlDataAdapter("SELECT Admin3207.UyeDetaylar.UyeAdi, Admin3207.UyeDetaylar.UyeSoyadi, dbo.SiparisDetaylari.SiparisID, dbo.Urunler.UrunAdi, dbo.Siparisler.SiparisTarihi, dbo.Siparisler.SiparisDurumu, dbo.Odemeler.OdemeTutari, dbo.Odemeler.OdemeSekli FROM dbo.Urunler INNER JOIN dbo.SiparisDetaylari ON dbo.Urunler.UrunID = dbo.SiparisDetaylari.UrunID INNER JOIN dbo.Siparisler ON dbo.SiparisDetaylari.SiparisID = dbo.Siparisler.SiparisID 	AND dbo.SiparisDetaylari.SiparisID = dbo.Siparisler.SiparisID INNER JOIN dbo.Odemeler ON dbo.Siparisler.SiparisID = dbo.Odemeler.SiparisID LEFT JOIN Admin3207.UyeDetaylar ON dbo.Siparisler.UserID = Admin3207.UyeDetaylar.UyeId where dbo.Urunler.UrunID=@UrunID", cnn);
                    adp.SelectCommand.Parameters.AddWithValue("@UrunID", comboBox1.SelectedValue);
                    dt = new DataTable();
                    adp.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAra_Click(object sender, EventArgs e)
        {
            try
            {
                adp = new SqlDataAdapter("SELECT Admin3207.UyeDetaylar.UyeAdi, Admin3207.UyeDetaylar.UyeSoyadi, dbo.SiparisDetaylari.SiparisID, dbo.Urunler.UrunAdi, dbo.Siparisler.SiparisTarihi, dbo.Siparisler.SiparisDurumu, dbo.Odemeler.OdemeTutari, dbo.Odemeler.OdemeSekli FROM dbo.Urunler INNER JOIN dbo.SiparisDetaylari ON dbo.Urunler.UrunID = dbo.SiparisDetaylari.UrunID INNER JOIN dbo.Siparisler ON dbo.SiparisDetaylari.SiparisID = dbo.Siparisler.SiparisID 	AND dbo.SiparisDetaylari.SiparisID = dbo.Siparisler.SiparisID INNER JOIN dbo.Odemeler ON dbo.Siparisler.SiparisID = dbo.Odemeler.SiparisID LEFT JOIN Admin3207.UyeDetaylar ON dbo.Siparisler.UserID = Admin3207.UyeDetaylar.UyeId where dbo.Urunler.UrunID=@UrunID or dbo.Siparisler.SiparisTarihi = @SiparisTarihi or Admin3207.UyeDetaylar.UyeAdi = @UyeAdi or Admin3207.UyeDetaylar.UyeSoyadi = @UyeSoyadi", cnn);
                adp.SelectCommand.Parameters.AddWithValue("@UrunID", comboBox1.SelectedValue);
                adp.SelectCommand.Parameters.AddWithValue("@SiparisTarihi", dateTimePicker1.Value);
                adp.SelectCommand.Parameters.AddWithValue("@UyeAdi", textBox1.Text);
                adp.SelectCommand.Parameters.AddWithValue("@UyeSoyadi", textBox1.Text);
                
                dt = new DataTable();
                adp.Fill(dt);
                dataGridView1.DataSource = dt; 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataView dv = new DataView(dt);
            try
            {
                dv.RowFilter = "UyeAdi LIKE '" + textBox1.Text + "%' OR UyeSoyadi='" + textBox1.Text + "'";
            }
            catch
            {
                dv.RowFilter = "UyeAdi LIKE '" + textBox1.Text + "%'";
            }
            dataGridView1.DataSource = dv;
        }

        private void btnGuncelle_Click(object sender, EventArgs e)
        {
            adp.UpdateCommand = new SqlCommand("UPDATE Odemeler SET dbo.Odemeler.OdemeTutari = @OdemeTutari, dbo.Odemeler.OdemeSekli = @OdemeSekli WHERE dbo.Odemeler.SiparisID = @SiparisID", cnn);

            SqlParameter prm = new SqlParameter("@OdemeTutari", SqlDbType.Money, 50, "OdemeTutari");
            SqlParameter prm2 = new SqlParameter("@OdemeSekli", SqlDbType.NVarChar, 50, "OdemeSekli");
            SqlParameter prm3 = new SqlParameter("@SiparisID", SqlDbType.Int, 9999, "SiparisID");


            adp.UpdateCommand.Parameters.Add(prm);
            adp.UpdateCommand.Parameters.Add(prm2);
            adp.UpdateCommand.Parameters.Add(prm3);

            

            adp.Update(dt);
            MessageBox.Show("Güncelleme Başarıyla Gerçekleştirildi.");
        }

        private void btnDegisiklikler_Click(object sender, EventArgs e)
        {
            dataGridView2.DataSource = dt.GetChanges();

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DataView dv = new DataView(dt);
            try
            {
                dv.RowFilter = "SiparisTarihi = '" + dateTimePicker1.Value.Date + "'";
            }
            catch (Exception EX)
            {
                MessageBox.Show("Hata : \n" + EX.Message);
            }
            dataGridView1.DataSource = dv;
        }


    }
}
